﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
	public sealed class CalendarDate : INotifyPropertyChanged
	{
		private DateTime _date;
		private bool _isOtherMonth;
		private bool _isSelectable = true;

		public CalendarDate(DateTime date)
		{
			_date = date;
		}

		public event PropertyChangedEventHandler PropertyChanged;

		public DateTime Date
		{
			get { return _date; }
			internal set
			{
				if (_date != value)
				{
					DateTime oldDate = _date;
					_date = value;
					OnPropertyChanged("Date");
					if (IsDateToday(oldDate) != IsDateToday(_date))
						OnPropertyChanged("IsToday");
					if (IsDateWeekend(oldDate) != IsDateWeekend(_date))
						OnPropertyChanged("IsWeekend");
				}
			}
		}

		public bool IsOtherMonth
		{
			get { return _isOtherMonth; }
			internal set
			{
				if (_isOtherMonth != value)
				{
					_isOtherMonth = value;
					OnPropertyChanged("IsOtherMonth");
				}
			}
		}

		public bool IsSelectable
		{
			get { return _isSelectable; }
			internal set
			{
				if (_isSelectable != value)
				{
					_isSelectable = value;
					OnPropertyChanged("IsSelectable");
				}
			}
		}

		public bool IsToday
		{
			get { return IsDateToday(_date); }
		}

		public bool IsWeekend
		{
			get { return IsDateWeekend(_date); }
		}

		public override string ToString()
		{
			return _date.ToShortDateString();
		}

		private void OnPropertyChanged(String info)
		{
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(info));
		}

		private bool IsDateWeekend(DateTime date)
		{
			return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday;
		}

		private bool IsDateToday(DateTime date)
		{
			return SinacorDatePicker.CompareYearMonthDay(date, DateTime.Now) == 0;
		}
	}
}